package server

import (
    "rpcserver/service/rpcservice"
    "rpcserver/handle"
    "rpcserver/config"
    "fmt"
)

type Server struct {
    id   int
    conf *config.Config
    rpc  *rpcservice.RpcService
}

func NewServer(id int) *Server {
    h := handle.NewSvHandle()
    r := rpcservice.NewRpcService(h)
    sv := &Server{id:id, conf:config.Instance(), rpc:r}
    return sv
}

func (sv *Server) Start() error {
    sv_cfg := sv.conf.GetServerConfig(sv.id)
    fmt.Printf("Server Start idx=%d at %s:%d\n", sv.id, sv_cfg.Address(), sv_cfg.Port())

    return sv.rpc.Start(sv_cfg.Address(), fmt.Sprintf("%d", sv_cfg.Port()))
}
